package com.gitee.jisong.superredis.ctrl

import com.gitee.jisong.common.CtrlRes
import com.gitee.jisong.spring.annotation.GETPOST
import com.gitee.jisong.superredis.pojo.RedisServers
import com.gitee.jisong.superredis.serv.ServersServ
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import java.time.LocalDateTime
import javax.servlet.http.HttpServletRequest

@RestController()
@RequestMapping("/api/redis/servers")
class ServersCtrl {

    val logger:Logger = LoggerFactory.getLogger(ServersCtrl::class.java)

    @Autowired lateinit var serversServ: ServersServ


    //查询服务器列表
    @GetMapping("/query/page")
    fun queryPage(
            @RequestParam("offset",required = false,defaultValue = "0") offset: Long,
            @RequestParam("limit",required = false , defaultValue = "50") limit: Long,
           httpRequest:HttpServletRequest
    ) : CtrlRes {
        logger.debug("offset:{},limit:{}",offset,limit)
        val pages = serversServ.queryPage(offset, limit)
        return CtrlRes(pages)
    }

    //新增服务器
    @PostMapping("/save/insert")
    fun saveInsert(@RequestBody redisServers: RedisServers,
            httpRequest:HttpServletRequest
    ) : CtrlRes {
        redisServers.createTime = LocalDateTime.now()
        redisServers.updateTime = LocalDateTime.now()
        redisServers.createUser = 1
        redisServers.updateUser = 2
        val res = serversServ.saveInsert(redisServers)
        return CtrlRes(res)
    }



    //编辑服务器
    @PostMapping("/save/update")
    fun saveUpdate(
            @RequestBody redisServers: RedisServers,
            httpRequest:HttpServletRequest
    ) : CtrlRes {
        val res = serversServ.saveUpdate(redisServers)
        return CtrlRes(res)
    }
    //删除服务器
}